﻿using System.Collections.Generic;
using System.Runtime.Remoting.Messaging;
using System.Web;
using System.Web.SessionState;
using System;
using System.Runtime.Serialization;
using System.Xml.Serialization;
using Newtonsoft.Json;

namespace SGRapidForm.Common
{
    [Serializable]
    public class ApplicationContext
    {
        public const string ContextKey = "SGRapidForm.Common.ApplicationContext";

        public const string ContextHeaderLocalName = "ApplicationContext";

        public const string ContextHeaderNamespace = "http://www.solutiongear.net/SGRapidForm/";

        public static ApplicationContext Current
        {
            get
            {
                //存在Sesssion中，就从其读取
                if (HttpContext.Current != null)
                {
                    if (HttpContext.Current.Session[ContextKey] == null)
                    {
                        HttpContext.Current.Session[ContextKey] = new ApplicationContext();
                    }
                    return HttpContext.Current.Session[ContextKey] as ApplicationContext;
                }

                //不存在属性集中，就存进去，最后返回
                if (CallContext.GetData(ContextKey) == null)
                {
                    CallContext.SetData(ContextKey, new ApplicationContext());
                }

                return CallContext.GetData(ContextKey) as ApplicationContext;
            }

            set
            {
                if (HttpContext.Current != null)
                {
                    HttpContext.Current.Session[ContextKey] = value;
                }
                else
                {
                    CallContext.SetData(ContextKey, value);
                }
            }
        }

        private string _sessionID = string.Empty;
        private string _userKeyid = string.Empty;
        private string _userName = string.Empty;
        private string _realName = string.Empty;
        private string _mobilePhone = string.Empty;
        private string _email = string.Empty;
        private string _passwordQuestion = string.Empty;
        private string _passwordAnswer = string.Empty;

        private string _deptKeyid = string.Empty;
        private string _deptCode = string.Empty;
        private string _deptName = string.Empty;
        private string _currentDeptKeyid = string.Empty;
        private string _currentDeptCode = string.Empty;
        private string _currentDeptName = string.Empty;
        private string _currentDeptType = string.Empty;
        private string _ownDeptKeyids = string.Empty;
        private string _ownDeptCodes = string.Empty;

        /// <summary>
        /// 会话ID
        /// </summary>
        public string SessionID
        {
            get { return _sessionID; }
            set { _sessionID = value; }
        }

        #region 登录用户信息

        /// <summary>
        /// 登录用户Keyid
        /// </summary>
        public string UserKeyid
        {
            get { return _userKeyid; }
            set { _userKeyid = value; }
        }

        /// <summary>
        /// 登录的用户名
        /// </summary>
        public string UserName
        {
            get { return _userName; }
            set { _userName = value; }
        }

        /// <summary>
        /// 登录用户的真实姓名
        /// </summary>
        public string RealName
        {
            get { return _realName; }
            set { _realName = value; }
        }

        /// <summary>
        /// 登录用户的手机
        /// </summary>
        public string MobilePhone
        {
            get { return _mobilePhone; }
            set { _mobilePhone = value; }
        }

        /// <summary>
        /// 登录用户的电子邮箱
        /// </summary>
        public string Email
        {
            get { return _email; }
            set { _email = value; }
        }

        /// <summary>
        /// 密保问题
        /// </summary>
        public string PasswordQuestion
        {
            get { return _passwordQuestion; }
            set { _passwordQuestion = value; }
        }

        /// <summary>
        /// 密保答案
        /// </summary>
        public string PasswordAnswer
        {
            get { return _passwordAnswer; }
            set { _passwordAnswer = value; }
        }

        /// <summary>
        /// 登录用户所属部门Keyid
        /// </summary>
        public string DeptKeyid
        {
            get { return _deptKeyid; }
            set { _deptKeyid = value; }
        }

        /// <summary>
        /// 登录用户所属部门编码
        /// </summary>
        public string DeptCode
        {
            get { return _deptCode; }
            set { _deptCode = value; }
        }

        /// <summary>
        /// 登录用户所属部门名称
        /// </summary>
        public string DeptName
        {
            get { return _deptName; }
            set { _deptName = value; }
        }

        #endregion 

        #region 选择登录的部门信息

        /// <summary>
        /// 当前选择登录的部门Keyid
        /// </summary>
        public string CurrentDeptKeyid
        {
            get { return _currentDeptKeyid; }
            set { _currentDeptKeyid = value; }
        }

        /// <summary>
        /// 当前选择登录的部门编码
        /// </summary>
        public string CurrentDeptCode
        {
            get { return _currentDeptCode; }
            set { _currentDeptCode = value; }
        }

        /// <summary>
        /// 当前选择登录的部门名称
        /// </summary>
        public string CurrentDeptName
        {
            get { return _currentDeptName; }
            set { _currentDeptName = value; }
        }

        /// <summary>
        /// 当前选择登录的部门类型
        /// </summary>
        public string CurrentDeptType
        {
            get { return _currentDeptType; }
            set { _currentDeptType = value; }
        }

        #endregion

        #region 登录用户可查看部门信息

        /// <summary>
        /// 当前用户可查看的部门Keyid
        /// </summary>
        /// <value>0101,010201,010202，0103</value>
        public string OwnDeptKeyids
        {
            get { return _ownDeptKeyids; }
            set { _ownDeptKeyids = value; }
        }

        /// <summary>
        /// 当前用户可查看的部门编码
        /// </summary>
        /// <value>福州分公司，厦门湖里办事处，厦门思明办事处，泉州分公司</value>
        public string OwnDeptCodes
        {
            get { return _ownDeptCodes; }
            set { _ownDeptCodes = value; }
        }

        #endregion
    }
}
